package org.brisling.common.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {

	private static MapUtil util;
	private MapUtil(){}
	
	private static MapUtil getUtil(){
		if(util ==null)
			util = new MapUtil();
		return util;
	}
	
	/**
	 * 根据传入map对象重新拼装条件map对象
	 * @param map
	 * @param keyName
	 * @param conditionName
	 * @param conditionMap
	 * @return
	 */
	public static Map<String,String> getMapCondition(Map<String,String> map,String keyName,String conditionName,Map<String,String> conditionMap){
		getUtil();
  		if((map==null) || (map.isEmpty())){
  			return conditionMap;
  		}
  		
  		if((keyName==null) || (keyName.trim().length()<=0)){
  			return conditionMap;
  		}
  		
  		if((conditionName==null) || (conditionName.trim().length()<=0)){
  			return conditionMap;
  		}
  		
  		conditionMap = conditionMap==null?new HashMap<String,String>():conditionMap;
  		if(map.containsKey(keyName)){
  			//获取条件值
  			String obj =map.get(keyName);
  			if((obj!=null) && (obj.trim().length()>0)){
  				conditionMap.put(conditionName,obj);	  				  				
  			}
  		}
  		
  		return conditionMap;
  	}
  	
	/**
	 * 根据传入map对象，通过反射从clazz对象中取出参数值并拼装条件参数对象
	 * @param map	参数定义
	 * @param clazz	反射实体类
	 * @param keyName	参数键值
	 * @param conditionName	与键值对应的条件名称
	 * @param conditionMap	已有条件map对象
	 * @return
	 */
  	public static Map<String,String> getReflectFiedCondition(Map<String,String> map,Object clazz,String keyName,String conditionName,Map<String,String> conditionMap){
  		getUtil();
  		
  		//判断参数是否有效
  		if((map==null) || (map.isEmpty())){
  			return conditionMap;
  		}
  		
  		if((keyName==null) || (keyName.trim().length()<=0)){
  			return conditionMap;
  		}
  		
  		if((conditionName==null) || (conditionName.trim().length()<=0)){
  			return conditionMap;
  		}
  		
  		//参数有消息判断结束
  		

  		
  		//避免条件为空
  		conditionMap = conditionMap==null?new HashMap<String,String>():conditionMap;
  		
  		
  		if(map.containsKey(keyName)){
  			//获取条件值
  			
  			String _value = map.get(keyName);
  			//判断条件值类型
  			if((_value!=null) && (_value.trim().length()>0)){
  				_value = _value.trim();
  				
  				
  				Integer _lv = LongUtil.getIntValue(_value, null);
  				
  				if(_lv==null){
  					//取数值失败，条件为字段名
  					Object obj =ReflectDataUtil.getFieldValue(map.get(keyName),clazz);
  		  			if((obj!=null) && (obj.toString().trim().length()>0)){
  		  				conditionMap.put(conditionName,obj.toString().trim());	  							
  		  			}
  				}else{
  					//数值
  					conditionMap.put(conditionName,_lv.toString());
  				}
  			}  			
  		}  		
  		return conditionMap;
  	}
  	
  	/**
	 * 获取传入的条件值
	 * @param body 前端 request 传入参数
	 * @return Map 名值对
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map<String,Object> getBodyParam(String body){
		HashMap<String,Object> map = new HashMap<String,Object>(); 
		if(body!=null){
			String _tmpBody =null;
			try {
				_tmpBody = java.net.URLDecoder.decode(body,"UTF-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
//			System.out.println(" tmp body:"+_tmpBody);
			String[] _param=_tmpBody.split("&");
			for(int i=0;i<_param.length;i++){
				if(_param[i]!=null && _param[i].indexOf("[")>0){
					//分隔符
					String splitStr = _param[i].substring(0,_param[i].indexOf("["));
					map.put(splitStr, getArrayMap((Map) map.get(splitStr),_param[i]));
					
				}else{
					//字段类
					String _paramNameValue[] = _param[i].split("=");
					
					if((_paramNameValue!=null) && (_paramNameValue.length>=2)){
					String _value = _paramNameValue[1];
					if(_value!=null){
	//					System.out.println("decode:" +_value);
						try {
							_value = java.net.URLDecoder.decode(_value,"UTF-8");
	//						System.out.println("decode:" +_value);
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						map.put(_paramNameValue[0], _value);
					}
					}
				}
				
			}
		}
		
		if(map.containsKey("search")){
			Map<String,Object> serachMap = (Map<String, Object>) map.get("search");
			if(!serachMap.isEmpty())
				map.put("condition", map.get("search"));
		}
		//确保map对象有 condition 对象
		if(!map.containsKey("condition")){
			map.put("condition", new HashMap<String,String>());
		}
		return map;
	}
	
	/**
	 * 获取集合类字符串的map映射
	 * @param map
	 * @param str
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static Map<String,String> getArrayMap(Map map,String str){
		
		if(map==null)
			map = new HashMap<String,String>();
		if(str!=null && str.trim().length()>0){
			str = str.trim();
			
			String splitStr = str.substring(str.indexOf("["));
			
			
			String _paramNameValue[] = splitStr.split("=");
			if(_paramNameValue.length==2){
				String _value = _paramNameValue[1];
				if(_value!=null){
	//					System.out.println("decode:" +_value);
					try {
						_value = java.net.URLDecoder.decode(_value,"UTF-8");
	//						System.out.println("decode:" +_value);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					String _field = _paramNameValue[0].substring(1, _paramNameValue[0].length()-1);
					map.put(_field, _value);
				}
			}			
		}
		return map;
	}

	
	/**
	 * 获取map值
	 * @param m		map对象
	 * @param key	键值
	 * @return
	 */
	public static String getMapData(Map<String,Object> m,String key){
		String ret = null;
		if( (null!=m) && (!m.isEmpty()) && (null!=key) ){
			if(m.containsKey(key)){
				Object o = m.get(key);
				if(o!=null){
					ret = o.toString();
				}
			}
		}
		return ret;
	}
}
